<?php

class Topic_m extends MY_Model {

    protected $_table_name = 'tbl_topics';
    protected $_primary_key = 'topic_id';
    protected $_primary_filter = 'intval';
    protected $_order_by = '';
    public $rules_topic = array(
        'topic_name' => array(
            'field' => 'topic_title',
            'label' => 'Topic Title',
            'rules' => 'trim|required|callback__unique_topic||xss_clean'),
        'topic_content' => array(
            'field' => 'topic_content',
            'label' => 'topic Content',
            'rules' => 'trim|required'),
    );

    public function get_new_topic() {
        $topic = new stdClass();
        $topic->topic_title = '';
        $topic->topic_content = '';
        $topic->topic_dated = '';
        $topic->topic_author = '';
        $topic->topic_status = '';
        return $topic;
    }

    public function array_from_post($fields) {
        $data = array();
        foreach ($fields as $field) {
            $data[$field] = $this->input->post($field);
        }
        return $data;
    }

    public function __construct() {
        parent::__construct();
    }

    // pagination 
    public function record_count() {
        return $this->db->count_all($this->_table_name);
    }

    public function fetch_topic($limit, $start) {
        $this->db->limit($limit, $start);
        $query = $this->db->get($this->_table_name);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }

}